package com.ruoyi.control.service.impl;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; //添加
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.core.text.StrFormatter;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.control.domain.Mainventilator1History;
import com.ruoyi.control.dto.ElectricityHistoryDto;
import com.ruoyi.control.dto.WaterDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.control.mapper.CentralpumphouseHistoryMapper;
import com.ruoyi.control.domain.CentralpumphouseHistory;
import com.ruoyi.control.service.ICentralpumphouseHistoryService;

/**
 * 中央水泵房Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-08
 */
@Service
public class CentralpumphouseHistoryServiceImpl extends ServiceImpl<CentralpumphouseHistoryMapper, CentralpumphouseHistory> implements ICentralpumphouseHistoryService //修改
{
    @Autowired
    private CentralpumphouseHistoryMapper centralpumphouseHistoryMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询中央水泵房
     *
     * @param id 中央水泵房主键
     * @return 中央水泵房
     */
    @Override
    public CentralpumphouseHistory selectCentralpumphouseHistoryById(String id) {
        return centralpumphouseHistoryMapper.selectCentralpumphouseHistoryById(id);
    }

    /**
     * 查询中央水泵房列表
     *
     * @param centralpumphouseHistory 中央水泵房
     * @return 中央水泵房
     */
    @Override
    public List<CentralpumphouseHistory> selectCentralpumphouseHistoryList(CentralpumphouseHistory centralpumphouseHistory) {
        return centralpumphouseHistoryMapper.selectCentralpumphouseHistoryList(centralpumphouseHistory);
    }

    /**
     * 新增中央水泵房
     *
     * @param centralpumphouseHistory 中央水泵房
     * @return 结果
     */
    @Override
    public int insertCentralpumphouseHistory(CentralpumphouseHistory centralpumphouseHistory) {
        return centralpumphouseHistoryMapper.insertCentralpumphouseHistory(centralpumphouseHistory);
    }

    /**
     * 修改中央水泵房
     *
     * @param centralpumphouseHistory 中央水泵房
     * @return 结果
     */
    @Override
    public int updateCentralpumphouseHistory(CentralpumphouseHistory centralpumphouseHistory) {
        centralpumphouseHistory.setUpdateTime(DateUtils.getNowDate());
        return centralpumphouseHistoryMapper.updateCentralpumphouseHistory(centralpumphouseHistory);
    }

    /**
     * 批量删除中央水泵房
     *
     * @param ids 需要删除的中央水泵房主键
     * @return 结果
     */
    @Override
    public int deleteCentralpumphouseHistoryByIds(String[] ids) {
        return centralpumphouseHistoryMapper.deleteCentralpumphouseHistoryByIds(ids);
    }

    /**
     * 删除中央水泵房信息
     *
     * @param id 中央水泵房主键
     * @return 结果
     */
    @Override
    public int deleteCentralpumphouseHistoryById(String id) {
        return centralpumphouseHistoryMapper.deleteCentralpumphouseHistoryById(id);
    }

    @Override
    public Integer[] selectByTagName(String tagName) {
        Integer[] history = new Integer[60 * 24];
        List<CentralpumphouseHistory> historyList = centralpumphouseHistoryMapper.selectByTagName(tagName);
        for (int i = 0; i < 60 * 24; i++) {
            if (i >= historyList.size()) {
                history[i] = 0;
            } else {
                double doubleValue = Double.parseDouble(historyList.get(i).getValue());
                int intValue = (int) doubleValue; // 如果需要四舍五入，可以使用 Math.round(doubleValue)
                history[i] = intValue;
            }
        }
        return history;
    }

    @Override
    public int getWaterTotal() {
        final String key = "psKey";
        int total = 0;
        String cachedValue = redisCache.getCacheObject(key);

        List<ElectricityHistoryDto> list = centralpumphouseHistoryMapper.getWaterTotal();
        total = list.size();
        redisCache.setCacheObject(key, String.valueOf(total));

//        if (cachedValue == null || cachedValue.isEmpty()) {
//            List<ElectricityHistoryDto> list = centralpumphouseHistoryMapper.getWaterTotal();
//            total = list.size();
//            redisCache.setCacheObject(key, String.valueOf(total));
//        } else {
//            total = Integer.parseInt(cachedValue);
//            System.out.println("读取psKey缓存: " + total);
//        }
        return Integer.parseInt(cachedValue);
    }


    @Override
    public List<String> getWeekWaterTotal() {
        List<WaterDto> dtoList = centralpumphouseHistoryMapper.getWeekWaterTotal();


        ArrayList<String> valueList = new ArrayList<>();
        if (dtoList.size() > 0){
            //遍历
            dtoList.stream().map(objInList -> {
                String total = objInList.getTotal();
                valueList.add(total);
                return valueList;
            }).collect(Collectors.toList());
        }else {
            valueList.add("0");
            valueList.add("0");
            valueList.add("0");
            valueList.add("0");
            valueList.add("0");
            valueList.add("0");
            valueList.add("0");
        }

        return valueList;
    }
}
